﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace MrBrooks.Utility
{
    /// <summary>
    /// 加解密工具
    /// </summary>
    public static class EncryptTools
    {
        #region .Md5.
        /// <summary>
        /// 加入掩码生成的MD5, 若使用标准Md5, 请调用 Md5Standard(source)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Md5(string source)
        {
            return Md5Standard(string.Format("19d425d1-48a7-{0}-486a-a4b3-9b0b8eaa0f76", source));
        }

        /// <summary>
        /// 标准Md5
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Md5Standard(string source)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(hashmd5.ComputeHash(Encoding.Default.GetBytes(source))).Replace("-", "").ToLower();
        }
        #endregion

        #region .可逆加解密.
        /// <summary>
        /// 简单的异或可逆加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="secretKey"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string EncryptSimple(string source, string secretKey, string encoding = "UTF-8")
        {
            byte[] sourceBytes = Encoding.GetEncoding(encoding).GetBytes(source);
            byte[] keyBytes = Encoding.GetEncoding(encoding).GetBytes(secretKey);
            int length = keyBytes.Length;

            int index = 0;
            StringBuilder sb = new StringBuilder();
            foreach (byte b in sourceBytes)
            {
                if (index == length)
                {
                    index = 0;
                }
                sb.AppendFormat("{0:X2}", b ^ keyBytes[index]);
                index++;
            }

            return sb.ToString();
        }

        /// <summary>
        /// 简单的异或可逆解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        public static string DecryptSimple(string source, string secretKey, string encoding = "UTF-8")
        {
            try
            {
                int sourceLength = source.Length / 2;
                byte[] sourceBytes = new byte[sourceLength];
                byte[] keyBytes = Encoding.GetEncoding(encoding).GetBytes(secretKey);
                int length = keyBytes.Length;

                int index = 0;
                for (int i = 0; i < sourceLength; i++)
                {
                    if (index == length)
                    {
                        index = 0;
                    }

                    byte b = (byte)Convert.ToInt16(source.Substring(i * 2, 2), 16);
                    sourceBytes[i] = (byte)(b ^ keyBytes[index]);
                    index++;
                }

                return Encoding.GetEncoding(encoding).GetString(sourceBytes, 0, sourceBytes.Length);
            }
            catch
            {
                return string.Empty;
            }
        }
        #endregion
    }
}
